#import "HZ_Search_ViewController.h"
#import <UIImageView+WebCache.h>
#import "Search_HZTableViewCell.h"
#import "CSL_Search_singleCell.h"//单曲
#import "CSL_Search_singerCell.h"//歌手
#import "CSL_Search_albumCell.h"//专辑
#import "CSL_Search_playlistCell.h"//歌单
#import "CSL_Search_MVCell.h"//MV
#import "WDGDatabaseTool.h"
#import "HZ_Search_Model.h"//专辑第一层Model;
#import "CSL_data_Model.h"//专辑第二层Model;
#import "Songer_Model.h"//歌手第一层Model
#import "Songer_data_Model.h"//歌手第二层Model
#import "Single_Model.h"//单曲第一层Model
#import "Single_dataModel.h"//单曲第二层Model
#import "Playlist_Model.h"//歌单的第一层Model
#import "Playlist_data_Model.h"//歌单的第二层Model
#import "Search_MV_Model.h"//MV第一层Model
#import "Search_MV_dataModel.h"//MV第二层Model
#import "Search_MV_Data_mvListModel.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
#import "CSLPlayer.h"

#import <MBProgressHUD.h>
#import "HZ_play_hs_Model.h"
#import "CSL_Player_ViewController.h"
#import "HZJSingerMusicViewController.h"
#import "HZJPlayListDetailViewController.h"

@interface HZ_Search_ViewController ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, RemoveselfDelegate >
@property (nonatomic, strong) HZ_play_hs_Model *playmodel;
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UITableView *Search_historyTableView;
@property (nonatomic, strong) UITableView *segmenTableView;
@property (nonatomic, strong) NSMutableArray *mutableArray;
@property (nonatomic, strong) NSMutableArray *titleArray;//存放数据库值得数组
@property (nonatomic, strong) WDGDatabaseTool *sousuoHandler;
@property (nonatomic, strong) WDGDatabaseTool *collectionHandler;
@property (nonatomic, strong) UISegmentedControl *segmentedControl;
@property (nonatomic, assign) NSInteger num;//判断页数的参数
@property (nonatomic, assign) NSInteger page;//上拉刷新判断页码的参数
@property (nonatomic, strong) HZ_Search_Model *searchModel;//专辑Model
@property (nonatomic, strong) Songer_Model *songerModel;//歌手Model
@property (nonatomic, strong) Single_Model *singleModel;//单曲Model
@property (nonatomic, strong) Playlist_Model *playlistModel;//歌单Model
@property (nonatomic, strong) Search_MV_Model *mvModel;//MV model;
@property (nonatomic, strong) Search_MV_dataModel *mv_dataModel;
@property (nonatomic, strong) UITextField *textField;

@property (nonatomic, strong) CSLPlayer *player;
@property (nonatomic, assign) BOOL isPlayer;
@property (nonatomic, strong) UIButton *stratPlayerButton;

//无网络的提示图
@property (nonatomic, strong) UIView *loadingView;
@property (nonatomic, strong) UIImageView *loadingImageView;
@property (nonatomic, strong) UILabel * loadingLabel;
@property (nonatomic, strong) UIButton *loadingButton;
//没搜到相关内容的提示图
@property (nonatomic, strong) UIImageView *noSearchImageView;
@property (nonatomic, strong) UILabel *noSearchLabel;


@property (nonatomic, strong) NSMutableArray *array;

//@property (nonatomic, assign) searchStyle searchStyle;//枚举取cell
@end

#define LISTURL @"?app=ttpod&v=v8.1.4.2016011318&user_id=0&mid=iPhone8,1&f=f320&s=s310&imsi=&hid=&splus=9.0.2&active=1&net=2&openudid=00e5496604ba470f229b34929ee14d0eb192a8e8&idfa=2301BEB2-C95C-4AA8-A905-794EC5CED975&utdid=VrPuK0MAZLUDAGfmA9wxwiTF&alf=201200&bundle_id=com.ttpod.music&latitude=38.882548834006&longtitude=121.5391476381413"

@implementation HZ_Search_ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.754 green:0.736 blue:0.769 alpha:1.000];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Back64"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(BackButtonAction:)];
#pragma mark - 创建搜索数据库
    [WDGDatabaseTool creatTableWithSQL:@"CREATE TABLE if not exists sousuo (id integer PRIMARY KEY AUTOINCREMENT,title text NOT NULL UNIQUE)"];
    self.sousuoHandler = [WDGDatabaseTool DBManageWithTableName:@"sousuo"];
    [WDGDatabaseTool openDebugMode:YES];
    [self CreateUISearchBar];
    [self CreateSearchhistoryTableView];
    [self CreatsegmentedControl];
    [self JudgetitleArray];
    _segmentedControl.hidden = YES;
    _segmenTableView.hidden = YES;
    [UIFont systemFontSize];
}

#pragma mark - 判断数据库里有无数据
-(void)JudgetitleArray{
    [_sousuoHandler openDatabase];
    self.mutableArray = [NSMutableArray arrayWithArray:[_sousuoHandler selectAllData]];
    self.titleArray = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < _mutableArray.count; i++) {
        NSDictionary *Dic = _mutableArray[i];
        [_titleArray addObject:Dic[@"title"]];
    }
    [_sousuoHandler closeDatabase];
    NSLog(@"%ld", _titleArray.count);
    if (_titleArray.count == 0) {
        _Search_historyTableView.hidden = YES;
    } else{
        _Search_historyTableView.hidden = NO;
    }
    [_Search_historyTableView reloadData];
}


#pragma mark - 返回按钮的点击事件
-(void)BackButtonAction:(UIBarButtonItem *)sender {
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 创建搜索控件
-(void)CreateUISearchBar {
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 0)];
    [_searchBar setPlaceholder:@"歌曲/歌手/歌单/MV"];//搜索框的占位符
    [_searchBar setPrompt:@""];//顶部提示文本，相当于控件的title
    [_searchBar setBarStyle:UIBarStyleDefault];//设置搜索框的样式
    [_searchBar setSpellCheckingType:UITextSpellCheckingTypeYes];//设置自动检查的类型
    _searchBar.delegate = self;//设置代理
    [_searchBar sizeToFit];
    self.navigationItem.titleView = _searchBar;
}

#pragma mark - 搜索所执行的方法
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [_sousuoHandler openDatabase];
    [_sousuoHandler insertDataWithDictionary:@{@"title" : _searchBar.text}];
    [_sousuoHandler closeDatabase];
    [_Search_historyTableView reloadData];
    _Search_historyTableView.hidden = YES;
    _segmentedControl.hidden = NO;
    _segmenTableView.hidden = NO;
    [_searchBar resignFirstResponder];
    if (_num == 0) {
        [self SingleRequestNet];
    } else if(_num == 1){
        [self SongerRequestNet];
    } else if(_num == 2){
        [self albumRequestNet];
    } else if(_num == 3){
        [self PlaylistRequestNet];
    } else if(_num == 4) {
        [self MVRequestNet];
    }
    
    [self SingleRequestNet];
    [self.segmenTableView reloadData];
}

#pragma mark - 创建UISegmentedControl
-(void)CreatsegmentedControl{
    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"单曲", @"歌手", @"专辑", @"歌单", @"MV"]];
    _segmentedControl.frame = CGRectMake(0, 0, self.view.bounds.size.width, 30);
    _segmentedControl.tintColor = [UIColor redColor];
    _segmentedControl.selectedSegmentIndex = 0;//设置当前选择的分段
    [_segmentedControl addTarget:self action:@selector(SegmentedControlAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_segmentedControl];
    _num = 0;
    _page = 1;
    [self CreateSegmenTableView];
}

#pragma mark -segmentedControl的点击事件
-(void)SegmentedControlAction:(UISegmentedControl *)sender{
    [self hiddenNoSearch];
    if (sender.selectedSegmentIndex == 0) {
        [self SingleRequestNet];
        _num = 0;
        [self.segmenTableView reloadData];
    } else if(sender.selectedSegmentIndex == 1) {
        [self SongerRequestNet];
        _num = 1;
        [_segmenTableView reloadData];
    }
    else if(sender.selectedSegmentIndex == 2) {
        [self albumRequestNet];
        _num = 2;
        [_segmenTableView reloadData];
    }
     else if(sender.selectedSegmentIndex == 3) {
        [self PlaylistRequestNet];
        _num = 3;
        [_segmenTableView reloadData];
    } else if(sender.selectedSegmentIndex == 4) {
        [self MVRequestNet];
        _num = 4;
        [_segmenTableView reloadData];
    }
}

#pragma mark - 创建UITableView
-(void)CreateSearchhistoryTableView {
    self.Search_historyTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style: UITableViewStylePlain];
    _Search_historyTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//分割线的样式，现在处于无分割线样式
    _Search_historyTableView.delegate = self;
    _Search_historyTableView.dataSource = self;
    [self CreateHeadView];
    [_Search_historyTableView registerClass:[Search_HZTableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];
    [_Search_historyTableView reloadData];
    [self.view addSubview:_Search_historyTableView];
    [self SingleRequestNet];
}

#pragma mark - 创建segmenTableView
-(void)CreateSegmenTableView{
    self.segmenTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, self.view.bounds.size.width, self.view.bounds.size.height - 60) style:UITableViewStyleGrouped];
    _segmenTableView.separatorStyle =  UITableViewCellSeparatorStyleSingleLineEtched;
    _segmenTableView.delegate = self;
    _segmenTableView.dataSource = self;
    [_segmenTableView registerClass:[CSL_Search_singleCell class] forCellReuseIdentifier:@"CSL_Search_singleCellIdentifier"];
    [self.view addSubview:_segmenTableView];
    [_segmenTableView registerClass:[CSL_Search_singerCell class] forCellReuseIdentifier:@"CSL_Search_singerCellIdentifier"];
    [_segmenTableView registerClass:[CSL_Search_albumCell class] forCellReuseIdentifier:@"CSL_Search_albumCellIdentifier"];
    [_segmenTableView registerClass:[CSL_Search_playlistCell class] forCellReuseIdentifier:@"CSL_Search_playlistCellIdentifier"];
    [_segmenTableView registerClass:[CSL_Search_MVCell class] forCellReuseIdentifier:@"CSL_Search_MVCellIdentifier"];
    [self CreateloadingView];
    [self HiddenloadingView];
    [self CreateNoSearch];
    [self hiddenNoSearch];
}

#pragma mark - cell的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _Search_historyTableView) {
        return _titleArray.count;
    } else if (tableView == _segmenTableView) {
        if(_num == 0){

                return _singleModel.data.count;

            if (_array.count != 0) {
                [self hiddenNoSearch];
                return _array.count;
            } else {
               [self appearNoSearch];
            }

        } else if(_num == 1){
                return _songerModel.data.count;
        } else if(_num == 2){
                return _searchModel.data.count;
        } else if(_num == 3) {
                return _playlistModel.data.count;
        } else if(_num == 4){
                return _mvModel.data.count;
        }
    }
    return 0;
}

#pragma mark - cell赋值
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _Search_historyTableView) {
        Search_HZTableViewCell *cell = [_Search_historyTableView dequeueReusableCellWithIdentifier:@"UITableViewCellIdentifier"];
        cell.titleLabel.text = _titleArray[indexPath.row];
        cell.removedelegata = self;
        return cell;
    } else if(tableView == _segmenTableView){
        [self hiddenNoSearch];
        if (_num == 0) {
            HZ_play_hs_Model *sing_dataModel = _array[indexPath.row];
            CSL_Search_singleCell *cell = [_segmenTableView dequeueReusableCellWithIdentifier:@"CSL_Search_singleCellIdentifier"];
            cell.titleLabel.text = sing_dataModel.name;
            cell.songerLabel.text = [NSString stringWithFormat:@"%@ ❤️%@", sing_dataModel.singerName, sing_dataModel.favorites];
            return cell;
        } else if (_num == 1) {
            Songer_data_Model *songDataModel = _songerModel.data[indexPath.row];
            CSL_Search_singerCell *cell = [_segmenTableView dequeueReusableCellWithIdentifier:@"CSL_Search_singerCellIdentifier"];
            [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:songDataModel.pic_url] placeholderImage:[[UIImage imageNamed:@"Music"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
            cell.titleLabel.text = songDataModel.singer_name;
            cell.singleLabel.text = [NSString stringWithFormat:@"%@首单曲", songDataModel.song_num];
            cell.albumLabel.text = [NSString stringWithFormat:@"%@张专辑", songDataModel.album_num];
            return cell;
        }
        else if (_num == 2) {
            CSL_data_Model *data_Model = _searchModel.data[indexPath.row];
            CSL_Search_albumCell *cell = [_segmenTableView dequeueReusableCellWithIdentifier:@"CSL_Search_albumCellIdentifier"];
            [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:data_Model.picUrl] placeholderImage:[[UIImage imageNamed:@"Music"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
            cell.titleLabel.text = data_Model.name;
            cell.nameEndTimaLabel.text = [NSString stringWithFormat:@"%@ %@", data_Model.singerName, data_Model.publishDate];
            return cell;
        }
    else if (_num == 3) {
            Playlist_data_Model *playlistDataModel = _playlistModel.data[indexPath.row];
            CSL_Search_playlistCell *cell = [_segmenTableView dequeueReusableCellWithIdentifier:@"CSL_Search_playlistCellIdentifier"];
            [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:playlistDataModel.pic_url] placeholderImage:[[UIImage imageNamed:@"Music"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
            cell.titleLabel.text =  playlistDataModel.title;
            cell.nameEndTimaLabel.text = [NSString stringWithFormat:@"被收听%@次", playlistDataModel.listen_count];
            
            return cell;
        } else if (_num == 4) {
//            self.mv_dataModel = _mvModel.data[indexPath.row];
            CSL_Search_MVCell *cell = [_segmenTableView dequeueReusableCellWithIdentifier:@"CSL_Search_MVCellIdentifier"];
            self.mv_dataModel = _mvModel.data[indexPath.row];
            cell.mv_dataModel = _mv_dataModel;
            return cell;
        }
    }
    return nil;
}

#pragma mark - 行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _Search_historyTableView) {
        return self.view.bounds.size.height/12;
    } else if(tableView == _segmenTableView){
        if (_num == 0) {
            return self.view.bounds.size.height/7.5;
        } else if(_num == 1){
            return self.view.bounds.size.height/7;
        }
        else if(_num == 2){
            return self.view.bounds.size.height/6;
        }
        else if(_num == 3) {
            return self.view.bounds.size.height/6;
        } else if(_num == 4) {
            return self.view.bounds.size.height/5.5;
        }
    }
    return 50;
}

#pragma mark - cell 的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (tableView == _Search_historyTableView) {
        [_Search_historyTableView reloadData];
        _searchBar.text = _titleArray[indexPath.row];
        [self SingleRequestNet];
        _Search_historyTableView.hidden = YES;
        _segmentedControl.hidden = NO;
        _segmenTableView.hidden = NO;
        [self hiddenNoSearch];
    } else if(tableView == _segmenTableView){
        if (_num == 0){
            if (_array.count > 0) {
                NSNotificationCenter *hssousuoArray = [NSNotificationCenter defaultCenter];
                
                NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
                
                [array addObject:_array];
                
                NSString *str = [NSString stringWithFormat:@"%ld", indexPath.row];
                [array addObject:str];
                
                [hssousuoArray postNotificationName:@"hssousuoArray" object:array userInfo:nil];
            }
            
            
            
            
            
            NSLog(@"🍎🍎🍎🍎");
            
        } else if(_num == 1) {
            if (_songerModel.data.count > 0) {
                Songer_data_Model *singerModel = _songerModel.data[indexPath.row];
                HZJSingerMusicViewController *singerVC = [[HZJSingerMusicViewController alloc] init];
                
                singerVC.urlString = [NSString stringWithFormat:@"%@", singerModel._id];
                singerVC.picUrl = singerModel.pic_url;
                singerVC.detailStr = singerModel.alias_name;
                [self.navigationController pushViewController:singerVC animated:YES];
            }
        }
        else if(_num == 2){
            if (_searchModel.data > 0) {
                CSL_data_Model *data_Model = _searchModel.data[indexPath.row];
                if (!data_Model.desc) {
                    
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"这个没有，看别的去" preferredStyle:UIAlertControllerStyleAlert];
                    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        
                    }]];
                    
                    [self presentViewController:alert animated:true completion:nil];
                    
                } else {
                HZJPlayListDetailViewController *singerVC = [[HZJPlayListDetailViewController alloc] init];
                singerVC.urlString = [NSString stringWithFormat:@"%@", data_Model.albumId];
                singerVC.picUrl = data_Model.picUrl;
                singerVC.detailStr = data_Model.name;
                [self.navigationController pushViewController:singerVC animated:YES];
                }
            }
        }
        else if(_num == 3){
            if (_playlistModel.data.count > 0) {
                Playlist_data_Model *playlist_data_Model = _playlistModel.data[indexPath.row];
                
                if (!playlist_data_Model.desc) {
                    
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"这个没有，看别的去" preferredStyle:UIAlertControllerStyleAlert];
                    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        
                    }]];
                    
                    [self presentViewController:alert animated:true completion:nil];
                    
                } else {
                
                HZJPlayListDetailViewController *singerVC = [[HZJPlayListDetailViewController alloc] init];
                singerVC.urlString = [NSString stringWithFormat:@"http://api.songlist.ttpod.com/songlists/%@%@", playlist_data_Model.quan_id, LISTURL];
                singerVC.picUrl = playlist_data_Model.pic_url;
                singerVC.detailStr = playlist_data_Model.title;
                [self.navigationController pushViewController:singerVC animated:YES];
                }
            }
        } else if(_num == 4){
            Search_MV_dataModel *mv_dataModel = _mvModel.data[indexPath.row];
            Search_MV_Data_mvListModel *mv_Data_mvListModel = mv_dataModel.mvList[0];
            CSL_Player_ViewController *player_ViewController = [[CSL_Player_ViewController alloc] init];
            player_ViewController.mv_Data_mvListModel = mv_Data_mvListModel;
            player_ViewController.mv_dataModel = _mvModel.data[indexPath.row];
            [self presentViewController:player_ViewController animated:YES completion:^{}];
            
        }
    }
}

#pragma mark - 返回section的头标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    if (tableView == _Search_historyTableView) {
        return @"搜索历史";
    } else if (tableView == _segmenTableView){
        if (_num == 0) {
            NSString *str = [NSString stringWithFormat:@"已经为你找到%@首相关歌曲", _singleModel.totalCount];
            return str;
        } else if(_num == 1){
            NSString *str = [NSString stringWithFormat:@"已经为你找到%@位相关歌手", _songerModel.rows];
            return str;
        }
        else if(_num == 2){
            NSString *str = [NSString stringWithFormat:@"已经为你找到%@张相关专辑", _searchModel.totalCount];
            return str;
        }
        else if(_num == 3){
            NSString *str = [NSString stringWithFormat:@"已经为你找到%@张相关歌单", _playlistModel.rows];
            return str;
        } else if(_num == 4) {
            NSString *str = [NSString stringWithFormat:@"已经为你找到%@个相关MV", _mvModel.rows];
            return str;
        }
    }
    return nil;
}

#pragma mark - tabieView 的头视图
-(void)CreateHeadView {
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height/16)];
    
    UIButton *RemoveSearchHistoryButton = [UIButton buttonWithType:UIButtonTypeSystem];
    RemoveSearchHistoryButton.frame = CGRectMake(bottomView.bounds.size.width/3.7, bottomView.bounds.size.height/10, bottomView.bounds.size.width/2, bottomView.bounds.size.height/1.5);
    RemoveSearchHistoryButton.backgroundColor = [UIColor redColor];
    [RemoveSearchHistoryButton setTitle:@"清除搜索历史" forState: UIControlStateNormal];
    [RemoveSearchHistoryButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //    设置圆角
    [RemoveSearchHistoryButton.layer setCornerRadius:15];
    RemoveSearchHistoryButton.layer.masksToBounds = YES;
    [RemoveSearchHistoryButton addTarget:self action:@selector(RemoveSearchHistoryButtonAction:) forControlEvents: UIControlEventTouchUpInside];
    [bottomView addSubview:RemoveSearchHistoryButton];
    self.Search_historyTableView.tableFooterView = bottomView;
}

#pragma mark - 头视图的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 25;
}

#pragma mark - cell上清除按钮的点击事件  (此处有bug)
-(void)Removeself {
    [self delete];
    [_titleArray removeLastObject];
    [_Search_historyTableView reloadData];
}

#pragma mark - 清除搜索历史按钮的点击事件
-(void)RemoveSearchHistoryButtonAction:(UIButton *)sender {
    [self delete];
//    [_titleArray removeAllObjects];
    [_Search_historyTableView reloadData];
    _Search_historyTableView.hidden = YES;
}

#pragma mark - fro循环删除_titleArray里面的数据
-(void)delete{
    [_sousuoHandler openDatabase];
    for (int i = 0; i < _titleArray.count; i ++) {
        WDGWhereCondition *wherecondition = [WDGWhereCondition conditionWithColumnName:@"title" Operator:@"=" Value:_titleArray[i]];
        [_sousuoHandler deleteDataWithWhereCondition:wherecondition];
    }
    [_sousuoHandler closeDatabase];
}

#pragma mark - 单曲网络请求
-(void)SingleRequestNet {
    
    NSString *strUrl = [NSString stringWithFormat:@"http://search.dongting.com/song/search?s=s200&cpu_model=MT6795&client_id=39855b1b8f39b17769adca6c7ec96dac&mid=nil&size=50&utdid=VtwfsEIOP2gDAFg7QtbwosWn&q=%@&rom=LetvFLe1s_CN252FX3253A5.0.2252FDBXCNOP5501001304S252F18253Auser252Frelease-keys&cpu=mt6795&v=v8.4.1.2016032512&imei=868897021103601&ram=2788536+kB&page=1&imsi=460004088355065&agent=none&longitude=121.54439&hid=2135253015108564&net=2&from=android&app=ttpod&os=5.0.2&alf=alf10001629&splus=5.0.2F21&api_version=1.0&active=0&tid=0&latitude=38.88345&f=f941&resolution=1080x1920&uid=868897021103601&language=zh&user_id=0", _searchBar.text ];
    [HSAFNetTool GETNetWithUrl:strUrl body:nil headerFile:nil response:HSJSON success:^(id result) {
        

        self.singleModel = [[Single_Model alloc] initWithDataSource:result];
        if (_singleModel.data.count == 0) {
            [self appearNoSearch];
            [self HiddenloadingView];
        } else {
        [MBProgressHUD showHUDAddedTo:_segmenTableView animated:YES];
        [self.segmenTableView reloadData];
        [MBProgressHUD hideHUDForView:_segmenTableView animated:YES];
        [self HiddenloadingView];
        }

        self.array = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in result[@"data"]) {
            HZ_play_hs_Model *model = [[HZ_play_hs_Model alloc] initWithDataSource:dic];
            [_array addObject:model];
        }
        
        [_segmenTableView reloadData];
        

           } failure:^(NSError *error) {
               [self hiddenNoSearch];
               [self AppearloadingView];
               [_segmenTableView reloadData];
               
    }];
}

#pragma mark -专辑网络请求
-(void)albumRequestNet {

    NSString *strUrl = [NSString stringWithFormat:@"http://search.dongting.com/album/search?&agent=none&size=50&longitude=121.54439&q=%@&net=2&from=android&os=5.0.2&v=v8.4.1.2016032512&alf=10001629&api_version=1.0&imei=868897021103601&latitude=38.88345&f=941&resolution=1080x1920&language=zh&page=1&user_id=0", _searchBar.text ];
    [HSAFNetTool GETNetWithUrl:strUrl body:nil headerFile:nil response: HSJSON success:^(id result) {
        
        self.searchModel = [[HZ_Search_Model alloc] initWithDataSource:result];
        if (_searchModel.data.count == 0) {
            [self appearNoSearch];
            [self HiddenloadingView];
        } else {
        [MBProgressHUD showHUDAddedTo:_segmenTableView animated:YES];
        [self.segmenTableView reloadData];
        [MBProgressHUD hideHUDForView:_segmenTableView animated:YES];
        [self HiddenloadingView];
        }
        } failure:^(NSError *error) {
            [self hiddenNoSearch];
            [self AppearloadingView];
    }];
}

#pragma mark - 歌手网络请求
-(void)SongerRequestNet {

    NSString *strUrl = [NSString stringWithFormat:@"http://search.dongting.com/artist/search?s=s200&size=50&hid=2135253015108564&utdid=VtwfsEIOP2gDAFg7QtbwosWn&q=%@&net=2&app=ttpod&v=v8.4.1.2016032512&alf=alf10001629&tid=0&uid=868897021103601&f=f941&page=1&imsi=460004088355065", _searchBar.text ];
[HSAFNetTool GETNetWithUrl:strUrl body:nil headerFile:nil response:HSJSON success:^(id result) {
    
    self.songerModel = [[Songer_Model alloc] initWithDataSource:result];
    if (_songerModel.data.count == 0) {
        [self appearNoSearch];
        [self HiddenloadingView];
    } else {
    [MBProgressHUD showHUDAddedTo:_segmenTableView animated:YES];
    [self.segmenTableView reloadData];
    [MBProgressHUD hideHUDForView:_segmenTableView animated:YES];
    [self HiddenloadingView];
    }
} failure:^(NSError *error) {
    [self hiddenNoSearch];
    [self AppearloadingView];
}];

}


#pragma mark - 歌单的网络请求
-(void)PlaylistRequestNet{
NSString *strUrl = [NSString stringWithFormat:@"http://search.dongting.com/songlist/search?s=s200&cpu_model=MT6795&mid=LetvX501&size=50&hid=2135253015108564&utdid=VtwfsEIOP2gDAFg7QtbwosWn&q=%@&net=2&app=ttpod&rom=Letv252FLe1s_CN252FX3253A5.0.2252FDBXCNOP5501001304S252F18253Auser252Frelease-keys&cpu=mt6795&v=v8.4.1.2016032512&splus=5.0.2252F21&alf=alf10001629&active=0&ram=2788536+kB&tid=0&resolution=1080x1920&uid=868897021103601&f=f941&page=1&imsi=460004088355065", _searchBar.text];
    [HSAFNetTool GETNetWithUrl:strUrl body:nil headerFile:nil response:HSJSON success:^(id result) {
        
        self.playlistModel = [[Playlist_Model alloc] initWithDataSource:result];
        if (_playlistModel.data.count == 0) {
            [self appearNoSearch];
            [self HiddenloadingView];
        } else {
        [MBProgressHUD showHUDAddedTo:_segmenTableView animated:YES];
        [self.segmenTableView reloadData];
        [MBProgressHUD hideHUDForView:_segmenTableView animated:YES];
        [self HiddenloadingView];
        }
    } failure:^(NSError *error) {
        [self hiddenNoSearch];
        [self AppearloadingView];
    }];
}

#pragma mark - MV网络请求
-(void)MVRequestNet{

    NSString *strUrl = [NSString stringWithFormat:@"http://search.dongting.com/mv/search?s=s200&size=50&hid=2135253015108564&utdid=VtwfsEIOP2gDAFg7QtbwosWn&q=%@&net=2&app=ttpod&v=v8.4.1.2016032512&alf=alf10001629&tid=0&uid=868897021103601&f=f941&page=1&imsi=460004088355065" , _searchBar.text];
    [HSAFNetTool GETNetWithUrl:strUrl body:nil headerFile:nil response:HSJSON success:^(id result) {
        
        self.mvModel = [[Search_MV_Model alloc] initWithDataSource:result];
        if (_mvModel.data.count == 0) {
            [self appearNoSearch];
            [self HiddenloadingView];
        } else {
        [MBProgressHUD showHUDAddedTo:_segmenTableView animated:YES];
        [self.segmenTableView reloadData];
        [MBProgressHUD hideHUDForView:_segmenTableView animated:YES];
        [self HiddenloadingView];
        }
    } failure:^(NSError *error) {
        [self hiddenNoSearch];
        [self AppearloadingView];
    }];
}

-(void)CreateloadingView{
    self.loadingView = [[UIView alloc] initWithFrame:_segmenTableView.bounds];
    _loadingView.backgroundColor = [UIColor whiteColor];
    [self.segmenTableView addSubview:_loadingView];
    self.loadingImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.segmenTableView.bounds.size.width/9, self.segmenTableView.bounds.size.height/15, self.segmenTableView.bounds.size.width/1.3, self.segmenTableView.bounds.size.height/1.5)];
    _loadingImageView.image = [UIImage imageNamed:@"jiazai"];
    //    _jiazaiImageView.backgroundColor = [UIColor redColor];
    [self.segmenTableView addSubview:_loadingImageView];
    self.loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.segmenTableView.bounds.size.width/4.2, self.segmenTableView.bounds.size.height/1.39, self.segmenTableView.bounds.size.width/1.8, self.segmenTableView.bounds.size.height/20)];
    //    jiazaiLabel.backgroundColor = [UIColor redColor];
    _loadingLabel.text = @"没有加载粗来，戳俺试试~~";
    _loadingLabel.textColor = [UIColor grayColor];
    [self.segmenTableView addSubview:_loadingLabel];
    self.loadingButton = [[UIButton alloc] initWithFrame:CGRectMake(self.segmenTableView.bounds.size.width/9, self.segmenTableView.bounds.size.height/15, self.segmenTableView.bounds.size.width/1.3, self.view.bounds.size.height/1.5)];
    [_loadingButton addTarget:self action:@selector(JiazaiButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.segmenTableView addSubview:_loadingButton];
}

-(void)HiddenloadingView{
    _loadingView.hidden = YES;
    _loadingImageView.hidden = YES;
    _loadingLabel.hidden = YES;
    _loadingButton.hidden = YES;
}
-(void)AppearloadingView{
    _loadingView.hidden = NO;
    _loadingImageView.hidden = NO;
    _loadingLabel.hidden = NO;
    _loadingButton.hidden = NO;
}

#pragma mark - 加载按钮的点击事件
-(void)JiazaiButtonAction:(UIButton *)sender {
    if (_num == 0) {
        [self SingleRequestNet];
    } else if (_num == 1) {
        [self SongerRequestNet];
    } else if (_num == 2) {
        [self albumRequestNet];
    } else if (_num == 3) {
        [self PlaylistRequestNet];
    } else if (_num == 4) {
        [self MVRequestNet ];
    }
}

#pragma mark - 没有搜索到相关内容的提示图
-(void)CreateNoSearch{
    self.noSearchImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.segmenTableView.bounds.size.width/6, self.segmenTableView.bounds.size.height/15,self.segmenTableView.bounds.size.width/1.5 , self.segmenTableView.bounds.size.height/2)];
//    _noSearchImageView.backgroundColor = [UIColor redColor];
    _noSearchImageView.image = [UIImage imageNamed:@"noSearch"];
    [self.segmenTableView addSubview:_noSearchImageView];
    self.noSearchLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.segmenTableView.bounds.size.width/4, self.segmenTableView.bounds.size.height/1.7,self.segmenTableView.bounds.size.width/2, self.segmenTableView.bounds.size.height/20)];
//    _noSearchLabel.backgroundColor = [UIColor redColor];
    _noSearchLabel.text = @"sonry!木有找到相关内容";
    _noSearchLabel.textColor = [UIColor colorWithRed:0.990 green:0.067 blue:0.113 alpha:0.5];
    _noSearchLabel.textAlignment = NSTextAlignmentCenter;
    [self.segmenTableView addSubview:_noSearchLabel];
}

#pragma mark - 
-(void)hiddenNoSearch{
    _noSearchImageView.hidden = YES;
    _noSearchLabel.hidden = YES;
}

-(void)appearNoSearch{
    _noSearchImageView.hidden = NO;
    _noSearchLabel.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
